﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace syc.Store
{
    public partial class 废料入出库管理 : System.Web.UI.Page
    {
        protected string Notice = "";
        string sql =
@"SELECT t1.ID
,t1.废料ID,
t2.废料名称,
t2.规格
,case when t1.入出=0 then
	case when t1.单位ID=1 then '一分厂'
	when t1.单位ID=2 then '二分厂'
	when t1.单位ID=3 then '三分厂'
	when t1.单位ID=4 then '四分厂'
	when t1.单位ID=5 then '五分厂'
	else
		'其他分厂'
	end
else
t3.名称
end as 供货或使用
,t1.日期
,cast(入出 as tinyint) as 入出
,cast(t1.数量 as float) as 数量
,cast(t1.单价 as float) as 单价
,cast(t1.结存数量 as float) as 结存数量
,cast(t1.结存金额 as float) as 结存金额
,t1.保管员
,t1.单据号

FROM 废料入出库 t1
left outer join 废料 t2 on t2.ID=t1.废料ID
left outer join 废料收货单位 t3 on t3.ID=t1.单位ID

";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.LeiBie.DataBind();
                this.ShouHuoDanWei.DataBind();
               
                this.Button_Query_Click(null, null);
            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }

        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.Button_Edit.Enabled = false;
            this.Button_Delete.Enabled = false;

      
            this.GridView1.SelectedIndex = -1;

            this.Button_Edit.Enabled = false;
            this.Button_Delete.Enabled = false;

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void PageSizeOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageSize = int.Parse(this.PageSizeOption.Text);
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Button_Delete.CommandName = this.GridView1.SelectedDataKey[0].ToString();
            this.Button_Delete.Enabled = true;

            this.Button_Edit.Enabled = true;
            string list = this.Button_Delete.CommandName;
            string result = Direct(list);
            string 单据号 = ((Label)this.GridView1.Rows[this.GridView1.SelectedIndex].FindControl("Label_djh")).Text;

          
        }
        private string GetState()
        {
            string state = "";

            if (this.LeiBie.SelectedItem.Value != "0")
            {
                state += " and t1.废料ID=" + this.LeiBie.SelectedItem.Value;

            }
            if (this.XianShi.Text != "0")
            {
                if (this.XianShi.Text == "1")
                {
                    state += " and t1.入出=" + 0;
                }
                if (this.XianShi.Text == "2")
                {
                    state += " and t1.入出=" + 1;
                }             
            }

            if (this.FenChang.Text != "0")
            {
                state += " and t1.单位ID=" + this.FenChang.Text;
            }

            if (this.ShouHuoDanWei.Text != "0")
            {
                state += " and t3.ID=" + this.ShouHuoDanWei.Text;
            }

            DateTime d;
            if (DateTime.TryParse(this.Date1.Text, out d))
            {
                state += " and t1.日期>='" + d.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out d))
            {
                state += " and t1.日期<='" + d.ToShortDateString() + "'";
            }

            if (state.StartsWith(" and"))
            {
                state = state.Substring(4);
            }
            if (state != "")
            {
                state = " where " + state;
            }
            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {

            string state = this.GetState();

            this.SqlText.Text = sql + state + " order by t1.ID desc";
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {

            this.LeiBie.Text = "0";
            this.FenChang.Text = "0";

            this.Date1.Text = "";
            this.Date2.Text = "";

        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.DeleteParameters["ID"].DefaultValue = this.Button_Delete.CommandName;
            if (this.SqlDataSource1.Delete() > 0)
            {
                this.Notice = "·删除成功";
            }
            else
            {
                this.Notice = "·删除失败";
            }
        }

   

        protected void Button_Edit_Click(object sender, EventArgs e)
        {
            string list = this.Button_Delete.CommandName;
            if (list == "")
            {
                this.Notice = "请点击表格前列的选择框，勾选要操作的记录。";
            }
            else
            {
                string result = Direct(list);
                if (result == "False")
                {
                    ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/废料入库_In.aspx?id=" + list + "',770,440);</script>");
                }
                else
                {
                    ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/废料出库_Out.aspx?id=" + list + "',770,460);</script>");
                }
            }
        }
        protected string Direct(string list)
        {
            string result;
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter("select 入出 from 废料入出库 where ID=" + list, Conn);
            sda.Fill(ds);
            result = ds.Tables[0].Rows[0][0].ToString();
            return result;
        }
    }
}
